package 第1周_线性查找法.线性查找法;

import 第1周_线性查找法.测试算法性能.ArrayGenerator;

public class LinearSearch {
    private LinearSearch(){}
    public static <E> int search(E[] data, E target){
       if(data == null || data.length == 0){
           return -1;
       }

       for (int i = 0; i < data.length; i++){
           if (data[i].equals(target)){
               return i;
           }
       }
       return -1;
   }

    public static void main(String[] args) {
        Integer[] arr1 = {1000000, 10000000};
        for (int n : arr1) {
            Integer[] data = ArrayGenerator.generateOrderedArray(n);

            //返回当前的时间戳
            long startTime = System.nanoTime();
            for (int i = 0; i < 100; i++) {
                LinearSearch.search(data, n);
            }
            long endTime = System.nanoTime();
            double time = (endTime - startTime) / 1000000000.0;

            System.out.println("n = " + n + ", 100 runs：" + time + "s");
        }

        /*System.out.println(res);

        Integer res2 = LinearSearch.search(data, 666);
        System.out.println(res2);*/

        /*Student[] students = {new Student("爱丽丝"),
                            new Student("Bobo"),
                            new Student("charles")};

        Student bobo = new Student("Bobo");
        int res3 = LinearSearch.search(students,bobo);
        System.out.println(res3);*/
    }
}
